<?php
/**
 * Yii view permit
 *
 * @author luubk
 * @since Jan 19, 2013 - 2:30:47 AM
 */
$auth   = Yii::app()->authManager;
$myRole = isset($_GET['role']) ? $auth->getAuthItem($_GET['role']) : current($auth->getRoles());
?>
<style>
    .radio.inline+.radio.inline, .checkbox.inline+.checkbox.inline{
        margin-left: 0;
    }
</style>
<?php if ($myRole): ?>
    <div class="row-fluid">
        <div class="box span12">
            <div class="box-header">
                <h2>
                    <i class="icon-user"></i><span class="break"></span>
                    Permissions for <?php echo $myRole->description ?>
                </h2>
                <div class="box-icon">                    
                    <a href="#" class="btn-minimize"><i class="icon-chevron-down"></i></a>                    
                </div>
            </div>
            <div class="box-content">

                <form method="POST" class="form-horizontal">
                    <fieldset>
                        <input type="hidden" name="role" value="<?php echo $myRole->name ?>" />

                        <div class="control-group">                            
                            <?php $lastOpera = false; ?>
                            <?php $group     = ''; ?>

                            <?php foreach ($auth->getOperations() as $opera): ?>                        
                                <?php
                                /**
                                 * Phân chia thành các operation theo các controller
                                 */
                                if (!$lastOpera)
                                {
                                    $lastOpera = $opera;
                                    $first     = TRUE;
                                }

                                /*
                                 * Các Operation có dạng Post.Create, ta cắt ra thành 2 phần
                                 */
                                $segment = explode('.', $opera->name);
                                $control = $segment[0];
                                $action  = $segment[1];

                                $lastSegment = explode('.', $lastOpera->name);
                                $lastControl = $lastSegment[0];

                                $lastOpera = $opera;

                                $check = '';
                                if ($myRole->hasChild($opera->name))
                                    $check = 'checked="checked"';

                                $opera_id = str_replace(".", "_", str_replace(".*", '', $opera->name));
                                ?>                 

                                <?php
                                /**
                                 * Hiển thị ra label đối với mỗi controller mới
                                 * 
                                 */
                                if ($control != $lastControl OR $first):
                                    ?>
                                    <?php echo ($first) ? '' : '</div></div><hr/><div class="control-group">'; ?>
                                    <label class="control-label">                                            
                                        <?php echo $opera_id ?>
                                    </label>
                                    <?php echo '<div class ="controls">'; ?>
                                    <?php $group = $opera_id ?>
                                <?php endif; ?>

                                <?php
                                // Tách tên operation từ 
                                $opera_name = str_replace("$group.", '', $opera->name);
                                ?>
                                <?php
                                // Các opera có tên khác * sẽ được để dạng inline
                                $inline     = $opera_name == '*' ? '' : 'inline'
                                ?>
                                <label class="checkbox <?php echo $inline ?> auth_checkbox">
                                    <input name="opera[]" 
                                           class="<?php echo $control ?> <?php echo $control . "_" . $action ?>"
                                           value="<?php echo $opera->name ?>" 
                                           type="checkbox" 
                                           id="<?php echo $opera_id ?>"
                                           parent="<?php echo $control ?>"
                                           <?php echo $check ?> />
                                           <?php echo $opera_name == '*' ? 'All' : $opera->description ?>
                                </label>
                                <?php $first      = FALSE ?>
                            <?php endforeach; ?>    
                        </div>
                        <button class="btn btn-primary">
                            <?php echo Yii::t('form', 'Save') ?>
                        </button>
                    </fieldset>
                </form>
            </div>
        </div>
    </div>

    <script>
        $('input[type=checkbox]').click(function() {
            id = $(this).attr('id');
            parent = $(this).attr('parent');

            if ($("#" + id).attr('checked') === 'checked')
            {
                $("." + id).attr('checked', 'checked');
                $("." + id).parent().addClass('checked');
            }
            else
            {
                $("." + id).removeAttr('checked');
                $("." + id).parent().removeClass('checked');
                $("#" + parent).removeAttr('checked');
                $("#" + parent).parent().removeClass('checked');
            }


        });
    </script>

    <?php

 endif;
